<template>
  <Description title="网速" content="根据网速判断是否关机" />

  <div>
    <div>下载：{{ speed.download }}kb/s</div>
    <a-form :model="formState" name="validate_other" v-bind="formItemLayout" @finishFailed="onFinishFailed"
      @finish="onFinish">
      <a-form-item label="类型" name="type" :rules="[{ required: true, message: 'Please select your country!' }]">
        <a-radio-group v-model:value="formState.type">
          <a-radio value="upload">上传</a-radio>
          <a-radio value="download">下载</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item name="speed" :rules="[{ required: true, message: 'Please select your country!' }]">
        <template #label>
          <div>速度</div>
        </template>
        <a-input v-model:value="formState.speed" type="number" suffix="kb/s" />
      </a-form-item>
      <a-form-item name="reminderMethod" label="操作"
        :rules="[{ required: true, message: 'Please select your country!' }]">
        <a-checkbox-group v-model:value="formState.reminderMethod">
          <a-row>
            <a-col :span="8">
              <a-checkbox value="systemNotice" style="line-height: 32px">系统通知</a-checkbox>
            </a-col>
            <a-col :span="8">
              <a-checkbox value="winTop" style="line-height: 32px">显示窗口</a-checkbox>
            </a-col>
            <a-col :span="8">
              <a-checkbox value="shutdown" style="line-height: 32px">关机</a-checkbox>
            </a-col>
          </a-row>
        </a-checkbox-group>
      </a-form-item>

      <template v-if="formState.reminderMethod.includes('systemNotice')">
        <a-form-item label="提示标题" name="title">
          <a-input v-model:value="formState.title" />
        </a-form-item>
        <a-form-item label="提示内容" name="body">
          <a-textarea v-model:value="formState.body" />
        </a-form-item>
      </template>

      <a-form-item :wrapper-col="{ span: 12, offset: 6 }">
        <a-button type="primary" html-type="submit">创建</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import {
  onBeforeMount,
  onBeforeUnmount,
  onMounted,
  onUnmounted,
  ref,
} from "vue";
import { reactive } from "vue";
import dayjs, { Dayjs } from "dayjs";
import Description from '@/components/Description.vue'
import { useTaskStore } from "@/stores/task";
const taskStore = useTaskStore();
const value = ref("");
const speed = ref({
  download: 0,
  upload: 0,
});
const formItemLayout = {
  labelCol: { span: 6 },
  wrapperCol: { span: 14 },
};

const formState = reactive<Record<string, any>>({
  time: "",
  speed: null,
  type: "",
  reminderMethod: [],
  title: "",
  body: "",
});
let timer: any;
let timer2: any;
let count = 0;

const onFinish = (values: any) => {
  console.log("Success:", values);
  const id = Date.now();
  const arr = [0, 0, 0];
  taskStore.data.push({
    id,
    type: "listener",
    url: "internetSpeed",
    task: window.ipcRenderer.on(
      "networkSpeed",
      (_, data) => {
        if (values.reminderMethod.includes("systemNotice") && arr[0] == 0) {
          arr[0] = 1;
          window.ipcRenderer.invoke("systemNotice", {
            title: values.title,
            body: values.body,
          });
        }
        if (values.reminderMethod.includes("winTop") && arr[1] == 0) {
          arr[1] = 1;

          window.ipcRenderer.invoke("setAlwaysOnTop", true);
          setTimeout(() => {
            window.ipcRenderer.invoke("setAlwaysOnTop", false);
          }, 100);
        }
        if (values.reminderMethod.includes("shutdown") && arr[2] == 0) {
          if (values.speed > data.download) {
            count += 2;
            if (count > 10) {
              arr[2] = 1;
              console.log("关机");
              // window.ipcRenderer.invoke("exec", "shutdown -s -t 0");
            }
          } else {
            count = 0;
          }
        }
        if (!arr.some((i) => i == 0)) {
          taskStore.data = taskStore.data.filter((i: any) => i.id != id);
        }
      },
      id,
    ),
    description: ''
  });
};

const a = Date.now();
onMounted(() => {
  window.ipcRenderer.on(
    "networkSpeed",
    (_, data) => {
      console.log(data);
      speed.value = data;
    },
    a,
  );
});

onBeforeUnmount(() => {
  window.ipcRenderer.off("networkSpeed", a);
  console.log("卸载");
});

// onMounted(() => {
//   window.ipcRenderer.on("networkSpeed", getSpeed);
//   // 卸载
//   onUnmounted(() => {
//     window.ipcRenderer.off("networkSpeed", getSpeed);
//     console.log("卸载");
//   });
// });

const onFinishFailed = (errorInfo: any) => {
  console.log("Failed:", errorInfo);
};
</script>

<style></style>
